﻿using System;
using System.Drawing;
using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Ctl;
using Medicalsystem.MCP.FormDesigner.Interface;

namespace Medicalsystem.MCP.FormDesigner
{
    /// <summary>
    ///控件捏手的处理方法-捏手控制控件的 Size - 一个控件容器里 有一组控件捏手
    /// </summary>
    public class UISizeKnob
    {
        private const int DOT_WIDTH = 7;   //UISizeDot宽度
        private const int DOT_HEIGHT = 7;  //UISizeDot高度
        private const int DOT_SPACE = -2;   //UISizeDot与_owner的距离

        private ContainerHandler _ctrlContainerHandler;

        private ctl_UISizeDot[] _UISizeDots;
        private int _clickAtX;
        private int _clickAtY;
        private bool _beginDrag;

        /// <summary>
        ///
        /// </summary>
        /// <param name="ctrlContainerHandler"></param>
        public UISizeKnob(ContainerHandler ctrlContainerHandler)
        {
            this._ctrlContainerHandler = ctrlContainerHandler;

            InitUISizeDots();
        }

        /// <summary>
        /// 属于的控件管理者
        /// </summary>
        public ContainerHandler CtrlContainerHandler
        {
            get { return _ctrlContainerHandler; }
            set { _ctrlContainerHandler = value; }
        }

        /// <summary>
        /// 注销
        /// </summary>
        public void Dispose()
        {
            for (int i = 0; i < this._UISizeDots.Length; i++)
            {
                this._UISizeDots[i].Dispose();
            }
        }

        private void InitUISizeDots()
        {
            this._UISizeDots = new ctl_UISizeDot[8];

            for (int i = 0; i < this._UISizeDots.Length; i++)
            {
                this._UISizeDots[i] = new ctl_UISizeDot();
                this._UISizeDots[i].Width = DOT_WIDTH;
                this._UISizeDots[i].Height = DOT_HEIGHT;
                this._UISizeDots[i].BackColor = Color.Black;
                this._UISizeDots[i].BorderColor = Color.White;
                this._UISizeDots[i].Visible = false;
                this._ctrlContainerHandler.Container.Controls.Add(this._UISizeDots[i]);
                this._UISizeDots[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.UISizeDot_MouseDown);
                this._UISizeDots[i].MouseMove += new System.Windows.Forms.MouseEventHandler(this.UISizeDot_MouseMove);
                this._UISizeDots[i].MouseUp += new System.Windows.Forms.MouseEventHandler(this.UISizeDot_MouseUp);
            }

            //设置类型

            this._UISizeDots[0].Cursor = System.Windows.Forms.Cursors.SizeNWSE;
            this._UISizeDots[0].Type = DotType.WestNorth;
            this._UISizeDots[1].Cursor = System.Windows.Forms.Cursors.SizeNS;
            this._UISizeDots[1].Type = DotType.North;
            this._UISizeDots[2].Cursor = System.Windows.Forms.Cursors.SizeNESW;
            this._UISizeDots[2].Type = DotType.EastNorth;
            this._UISizeDots[3].Cursor = System.Windows.Forms.Cursors.SizeWE;
            this._UISizeDots[3].Type = DotType.East;
            this._UISizeDots[4].Cursor = System.Windows.Forms.Cursors.SizeNWSE;
            this._UISizeDots[4].Type = DotType.EastSouth;
            this._UISizeDots[5].Cursor = System.Windows.Forms.Cursors.SizeNS;
            this._UISizeDots[5].Type = DotType.South;
            this._UISizeDots[6].Cursor = System.Windows.Forms.Cursors.SizeNESW;
            this._UISizeDots[6].Type = DotType.WestSouth;
            this._UISizeDots[7].Cursor = System.Windows.Forms.Cursors.SizeWE;
            this._UISizeDots[7].Type = DotType.West;
        }

        /// <summary>
        /// 设置捏手的呈现
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="show">呈现状态</param>
        /// <param name="setposition">是否重置位置</param>
        public void ShowUISizeDots(System.Windows.Forms.Control ctrl, bool show, bool setposition)
        {
            /*
             * 当前设计：焦点控件显示捏手
             * 焦点切换时，捏手 隐藏 与 显示
             */

            if (setposition)
            {
                SetUISizeDotsPosition(ctrl);
            }

            this._ctrlContainerHandler.Container.SuspendLayout();

            if (show)
            {
                //根据焦点控件
                switch ((ctrl as ICtrlBase).UISizeMode)
                {
                    case EUISizeMode.Null:
                        foreach (ctl_UISizeDot UISD in this._UISizeDots)
                        {
                            UISD.Visible = false;
                        }
                        break;

                    case EUISizeMode.AllowNone:
                        foreach (ctl_UISizeDot UISD in this._UISizeDots)
                        {
                            if (UISD.Type == DotType.WestNorth)
                            {
                                UISD.Visible = true;
                            }
                            else
                            {
                                UISD.Visible = false;
                            }
                        }
                        break;

                    case EUISizeMode.AllowWidth:
                        foreach (ctl_UISizeDot UISD in this._UISizeDots)
                        {
                            if (UISD.Type == DotType.East || UISD.Type == DotType.West)
                            {
                                UISD.Visible = true;
                            }
                            else
                            {
                                UISD.Visible = false;
                            }
                        }
                        break;

                    case EUISizeMode.AllowBoth:
                        foreach (ctl_UISizeDot UISD in this._UISizeDots)
                        {
                            UISD.Visible = true;
                        }
                        break;

                    default:
                        break;
                }
            }
            else
            {
                foreach (ctl_UISizeDot UISD in this._UISizeDots)
                {
                    UISD.Visible = false;
                }
            }

            this._ctrlContainerHandler.Container.ResumeLayout(false);
        }

        /// <summary>
        /// 将控件带到 Z 顺序的前面
        /// </summary>
        public void BringToFront()
        {
            foreach (ctl_UISizeDot UISD in this._UISizeDots)
            {
                UISD.BringToFront();
            }
        }

        /// <summary>
        /// 设置控件捏手的位置-根据焦点控件设置
        /// </summary>
        /// <param name="ctrl"></param>
        public void SetUISizeDotsPosition(System.Windows.Forms.Control ctrl)
        {
            int left, width, height, top;
            left = ctrl.Left;
            top = ctrl.Top;
            width = ctrl.Width;
            height = ctrl.Height;

            switch ((ctrl as ICtrlBase).UISizeMode)
            {
                case EUISizeMode.AllowNone:
                    foreach (ctl_UISizeDot UISD in this._UISizeDots)
                    {
                        if (UISD.Type == DotType.WestNorth)
                        {
                            UISD.Location = new Point(left - DOT_WIDTH - DOT_SPACE, top - DOT_HEIGHT - DOT_SPACE);
                            break;
                        }
                    }
                    break;

                case EUISizeMode.AllowWidth:
                    foreach (ctl_UISizeDot UISD in this._UISizeDots)
                    {
                        if (UISD.Type == DotType.East)
                        {
                            UISD.Location = new Point(left + width + DOT_SPACE, top + height / 2 - DOT_HEIGHT / 2);
                        }
                        else if (UISD.Type == DotType.West)
                        {
                            UISD.Location = new Point(left - DOT_WIDTH - DOT_SPACE, top + height / 2 - DOT_HEIGHT / 2);
                        }
                    }
                    break;

                case EUISizeMode.AllowBoth:
                    foreach (ctl_UISizeDot UISD in this._UISizeDots)
                    {
                        switch (UISD.Type)
                        {
                            case DotType.WestNorth:
                                UISD.Location = new Point(left - DOT_WIDTH - DOT_SPACE, top - DOT_HEIGHT - DOT_SPACE);
                                break;

                            case DotType.North:
                                UISD.Location = new Point(left + width / 2 - DOT_WIDTH / 2, top - DOT_HEIGHT - DOT_SPACE);
                                break;

                            case DotType.EastNorth:
                                UISD.Location = new Point(left + width + DOT_SPACE, top - DOT_HEIGHT - DOT_SPACE);
                                break;

                            case DotType.East:
                                UISD.Location = new Point(left + width + DOT_SPACE, top + height / 2 - DOT_HEIGHT / 2);
                                break;

                            case DotType.EastSouth:
                                UISD.Location = new Point(left + width + DOT_SPACE, top + height + DOT_SPACE);
                                break;

                            case DotType.South:
                                UISD.Location = new Point(left + width / 2 - DOT_WIDTH / 2, top + height + DOT_SPACE);
                                break;

                            case DotType.WestSouth:
                                UISD.Location = new Point(left - DOT_WIDTH - DOT_SPACE, top + height + DOT_SPACE);
                                break;

                            case DotType.West:
                                UISD.Location = new Point(left - DOT_WIDTH - DOT_SPACE, top + height / 2 - DOT_HEIGHT / 2);
                                break;
                            default:
                                break;
                        }
                    }
                    break;

                default:
                    break;
            }
        }

        #region 移动操作

        private void UISizeDot_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                this._beginDrag = true;
                this._clickAtX = e.X;
                this._clickAtY = e.Y;
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        private void UISizeDot_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                if (this._beginDrag)
                {
                    //下面代码将修改控件属性值 会触发控件属性改变事件 而此处不需要该事件的执行

                    int eX = e.X - this._clickAtX;
                    int eY = e.Y - this._clickAtY;

                    foreach (CtrlMngDesign ch in this._ctrlContainerHandler.ModelDesign.SelectList)
                    {
                        switch (((ctl_UISizeDot)sender).Type)
                        {
                            case DotType.WestNorth:
                                ch.Owner.Location = new System.Drawing.Point(ch.Owner.Left + eX, ch.Owner.Top + eY);
                                ch.Owner.Size = new System.Drawing.Size(ch.Owner.Width - eX, ch.Owner.Height - eY);
                                break;

                            case DotType.North:
                                ch.Owner.Location = new System.Drawing.Point(ch.Owner.Left, ch.Owner.Top + eY);
                                ch.Owner.Size = new System.Drawing.Size(ch.Owner.Width, ch.Owner.Height - eY);
                                break;

                            case DotType.EastNorth:
                                ch.Owner.Location = new System.Drawing.Point(ch.Owner.Left, ch.Owner.Top + eY);
                                ch.Owner.Size = new System.Drawing.Size(ch.Owner.Width + eX, ch.Owner.Height - eY);
                                break;

                            case DotType.East:
                                ch.Owner.Size = new System.Drawing.Size(ch.Owner.Width + eX, ch.Owner.Height);
                                break;

                            case DotType.EastSouth:
                                ch.Owner.Size = new System.Drawing.Size(ch.Owner.Width + eX, ch.Owner.Height + eY);
                                break;

                            case DotType.South:
                                ch.Owner.Size = new System.Drawing.Size(ch.Owner.Width, ch.Owner.Height + eY);
                                break;

                            case DotType.WestSouth:
                                ch.Owner.Location = new System.Drawing.Point(ch.Owner.Left + eX, ch.Owner.Top);
                                ch.Owner.Size = new System.Drawing.Size(ch.Owner.Size.Width - eX, ch.Owner.Height + eY);
                                break;

                            case DotType.West:
                                ch.Owner.Location = new System.Drawing.Point(ch.Owner.Left + eX, ch.Owner.Top);
                                ch.Owner.Size = new System.Drawing.Size(ch.Owner.Width - eX, ch.Owner.Height);
                                break;
                            default:
                                break;
                        }
                    }

                    this._ctrlContainerHandler.Container.Refresh();
                }
            }
            catch (Exception ex)
            {
                TryCustomException.TransactException(ex);
            }
        }

        private void UISizeDot_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this._beginDrag = false;
        }

        #endregion 移动操作
    }
}